package SI::Person;

use Moo;

use utf8::all;
use DateTime;
use strict;
use warnings;

use SI::Person::BaseInfo;
use SI::Person::InsurInfo;
use SI::Person::Nation;
use SI::Person::Salary;
use SI::Person::Status;
use SI::Company::BaseInfo;

use SI::Record::NewInsuring;

has 'sn' => (is=>'rw');


sub find_sn {
    my ($class, $field, $value) = @_;

    my $grep = grep $field, ('id', 'name') ;
    
    die "Field should be id or name!" unless $grep;

    my $base = 
       SI::Person::BaseInfo::Manager->
           get_baseinfos(
               query => [ $field => { eq => $value }]);

    my @sn = map {$_->sn} @$base or return;

    return \@sn;
}

sub count_sn {
    my ($class, $field, $value) = @_;

    my $grep = grep $field, ('id', 'name');

    die "Field should be id or name!" unless $grep;

    my $count = 
       SI::Person::BaseInfo::Manager->
           get_baseinfos_count(
               query => [$field => { eq => $value }]);
}



# 通过个人编号加载人员基本信息
sub get_base {
    my $self = shift;

    my $sn = $self->sn or die "You should set up sn of a Person.";

    my $base = SI::Person::BaseInfo->new(sn=>$sn) ;

    $base->load or die "No such  Person!";
    
    return $base;
}

# 获取单位信息
sub get_company {
    my $self = shift;

    my $sn = $self->sn or die "You should set up sn of a Person.";
    
    my $com_id = $self->get_base->company_id;

    my $company = SI::Company::BaseInfo->new(id => $com_id);

    return $company;
}

# 更新人员基本信息
sub set_base {
    my ($self, $new_info) = @_;
    
    my $base = $self->get_base
      or die "You should build a new baseinfo first!";

    my @fields = ('id','name','sex','nation_code','company_id',
      'tel','address','postcode','status_code');

    for my $f (@fields) {
        
      if ( my $val = $new_info->{$f} ) {
        no strict 'refs';
        &{"SI::Person::BaseInfo::$f"}($base,$val);
      }

    }

    $base->save or die "Update Baseinfo Failed!";  
    
    return $base;
}


# 新增人员
sub new_base {
  my ($self, $baseinfo) = @_;

  my $base = SI::Person::BaseInfo->new(
      sn => $baseinfo->{sn},
      id => $baseinfo->{id},
      name => $baseinfo->{name},
      sex => $baseinfo->{sex},
      nation_code => $baseinfo->{nation_code},
      company_id => $baseinfo->{company_id},
      tel => $baseinfo->{tel},
      postcode => $baseinfo->{postcode},
      status_code => $baseinfo->{status_code});

  $base->save or die "Insert a Person baseinfo into DB Failed!";

  $self->sn($base->sn);

  return $base;

}


# 删除人员
sub del_base {
    my $self = shift;

    $self->get_base()->delete() or return;

    return 1;
}

#人员参保登记
sub reg_insure {
  ...
}


# 人员收入登记
sub reg_salary { ... }

# 人员中断参保
sub stop_insure  {
    my $self = shift;

    my $stat = $self->baseinfo->status_code;
    
    $self->set_base({status_code=>'02'}) 
        if $stat eq '01';
    
    $self->set_base({status_code=>'05'}) 
        if $stat eq '04';

    if ($stat ne '01' && $stat ne '04') {
        warn "Notice: ONLY status code is 01 or 04 CAN stop insurance!";
        return;
    }

    return 1;
}

# 人员终止参保
sub end_insure {
  my $self = shift;
  
  my $stat = $self->get_base->status_code;
  $self->set_base({status_code=>'03'}) 
      if $stat eq '02';
  $self->set_base({status_code=>'06'})
      if $stat eq '05';
  if ($stat ne '02' && $stat ne '05') {
      warn "Notice: ONLY status code is 02 or 05 CAN end insurance!";
      return;
  }

  return 1;
}

# 人员转退休
sub insure_to_retire { ... }








1;
